package com.mlnx.work.controller;

import com.mlnx.common.annotations.NeedLogin;
import com.mlnx.common.entity.Response;
import com.mlnx.work.config.RightKeys;
import com.mlnx.work.entity.Role;
import com.mlnx.work.pojo.form.RoleFilterForm;
import com.mlnx.work.pojo.form.RoleForm;
import com.mlnx.work.service.RoleService;

import org.shan.spring.base.RdBaseController;
import org.shan.spring.base.annotations.SysLog;
import org.shan.spring.mybatis.service.ICurdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * Created by amanda.shan on 2020/5/19.
 */
@SysLog(title = "用户角色")
@Api(value = "角色接口", tags = {"角色接口"})
@RestController
@RequestMapping("/role")
public class RoleController extends RdBaseController<Role, RoleFilterForm> {

    @Autowired
    private RoleService roleService;

    @Override
    public ICurdService getService() {
        return roleService;
    }

    @Override
    public Role newInstance(Integer id) {
        Role role = new Role();
        role.setId(id);
        return role;
    }

    @NeedLogin(permissions = {RightKeys.RIGHT_ROLE_MANAGER})
    @ApiOperation(
            value = "新增",
            notes = ""
    )
    @PostMapping({""})
    public Response add(@Valid @RequestBody RoleForm form) throws IllegalAccessException, InstantiationException {
        roleService.save(form);
        return this.result();
    }

    @NeedLogin(permissions = {RightKeys.RIGHT_ROLE_MANAGER})
    @ApiOperation(
            value = "更改",
            notes = ""
    )
    @PutMapping({"/{id}"})
    public Response modify(@PathVariable("id") Integer id, @Valid @RequestBody RoleForm form) {
        roleService.updateById(id, form);
        return this.result();
    }

}
